import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/my/my_purchase_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/view/my/widgets/purchase_item.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import 'package:sports_new/widgets/loading/no_data_widget.dart';

import '../../widgets/base/qkd_text.dart';

class MyPurchasePage extends StatefulWidget {
  const MyPurchasePage({super.key});

  @override
  State<MyPurchasePage> createState() => _MyPurchasePageState();
}

class _MyPurchasePageState extends State<MyPurchasePage> {
  final MyPurchaseLogic logic = Get.put(MyPurchaseLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MyPurchaseLogic>(
      builder: (logic) {
        return Scaffold(
          appBar: Styles.appBar(title: const QkdText('我的订单')),
          backgroundColor: Colours.scaffoldBg,
          body: Column(
            children: [
              Container(
                color: Colours.white,
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Styles.tabbar(
                        controller: logic.tabController,
                        indicatorBottom: -6,
                        labelPadding: const EdgeInsets.symmetric(horizontal: 6),
                        scrollable: true,
                        tabs: logic.tabs.map((e) => QkdText(e)).toList()
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: EasyRefresh(
                  onRefresh: logic.getRefresh,
                  onLoad: logic.loadMore,
                  footer: Styles.refreshFooter(position: IndicatorPosition.locator),
                  controller: logic.refreshController,
                  child: TabBarView(
                    controller: logic.tabController,
                    children: List.generate(
                      logic.tabs.length,
                      (index) => LoadingWidget<bool>(
                        isLoading: logic.pageList[index] == null,
                        noData: const NoDataWidget(
                          tip: '你还没有订单哦',
                        ),
                        data: logic.pageList[index]?.length == 0,
                        child: () => ListView(
                          children: List<Widget>.generate(
                            logic.pageList[index]?.length ?? 0,
                              (i) => PurchaseItem(
                                data: logic.pageList[index]![i],
                                type:PurchaseType.values[index]
                            )
                          ).append(const FooterLocator())
                        ),
                      )
                    )
                  )
                ),
              ),
            ],
          ),
        );
      }
    );
  }
}
